
#include "main.h"

//=========================================================
//=========================================================
unsigned char Flash_Write_128Bytes(unsigned char *tab,unsigned long addr)
{
	u8 i, Verity_Flag=0;
	u32 buf[32];
	
	for(i=0; i<32; i++)
	{
		buf[i] = tab[i*4] + ((unsigned long)tab[i*4+1]<<8) + ((unsigned long)tab[i*4+2]<<16) + ((unsigned long)tab[i*4+3]<<24);
	}
	
	FLASH_Unlock_Fast();
	FLASH_ErasePage_Fast(addr);
	//printf("128Byte Page Erase Sucess\r\n");
	
	FLASH_BufReset();
	FLASH_BufLoad(addr, buf[0], buf[1], buf[2], buf[3]);
	FLASH_BufLoad(addr + 0x10, buf[4], buf[5], buf[6], buf[7]);
	FLASH_BufLoad(addr + 0x20, buf[8], buf[9], buf[10], buf[11]);
	FLASH_BufLoad(addr + 0x30, buf[12], buf[13], buf[14], buf[15]);
	FLASH_BufLoad(addr + 0x40, buf[16], buf[17], buf[18], buf[19]);
	FLASH_BufLoad(addr + 0x50, buf[20], buf[21], buf[22], buf[23]);
	FLASH_BufLoad(addr + 0x60, buf[24], buf[25], buf[26], buf[27]);
	FLASH_BufLoad(addr + 0x70, buf[28], buf[29], buf[30], buf[31]);	
	FLASH_ProgramPage_Fast(addr);
	//printf("128Byte Page Program Sucess\r\n");
	
  FLASH_Lock_Fast();	
	
	for(i=0; i<32; i++)
	{
		if(buf[i] == *(u32*)(addr + 4*i)){
			Verity_Flag = 0;
		}
		else 
		{
			Verity_Flag = 1;
			break;
		}
	}
	
	//if(Verity_Flag) printf("128Byte Page Verity Fail\r\n");
	//else printf("128Byte Page Verity Sucess\r\n");
	
	return Verity_Flag;
}
